package example

import (
	"database/sql"
	"fmt"
	"game/game-db/mysql/dao"
	"game/game-db/orm/metag"
	"time"
)

type ExampleAutowireEntity struct {
	ExampleParent
	id          int64                   `orm:"id=AUTOWIRE;autoBase=100000;name=id;comment=唯一ID"`
	name        string                  `orm:"comment=名字;length=255"`
	info        string                  `orm:"comment=样例;extra=text;readonly=true"`
	dailyTime   time.Time               `orm:"name=daily_time;comment=每日重置时间"`
	equips      map[int]string          `orm:"comment=装备信息"`
	grade       int32                   `orm:"comment=班级"`
	num         int64                   `orm:"comment=学号"`
	has         bool                    `orm:"comment=是否有"`
	LongTime    int64                   `orm:"name=long_time;comment=时间例子;extra=time"`
	StructInfo  ExampleStruct           `orm:"name=struct_info;comment=结构体例子;length=1000"`
	StructSlice []ExampleStruct         `orm:"name=struct_slice;comment=结构体切片例子;length=1000"`
	StructMap   map[int32]ExampleStruct `orm:"name=struct_map;comment=结构体映射例子;extra=text"`
	IntSlice    []int32                 `orm:"name=int_slice;comment=数字切片例子;length=1000"`
	StringSlice []string                `orm:"name=string_slice;comment=字符串切片例子;length=text"`
	F32         int32                   `orm:"comment=浮点32例子"`
	F64         float64                 `orm:"comment=浮点64例子;extra=30,10"`
	FFF         float64                 `orm:"comment=浮点64例子"`
}

func (t *ExampleAutowireEntity) TableMetag() metag.SplitTableMetag {
	tableTag := metag.SplitTableMetag{
		TableMetag: metag.TableMetag{
			Name:    "example_autowire_entity",
			Comment: "例子",
			Indexes: make([]*metag.IndexMetag, 0),
		},
		SplitWay: metag.DAY,
	}
	tableTag.Indexes = append(tableTag.Indexes, &metag.IndexMetag{
		Fields: []string{"grade"},
	})
	tableTag.Indexes = append(tableTag.Indexes, &metag.IndexMetag{
		Fields: []string{"id", "grade"},
	})
	return tableTag
}

func (t *ExampleAutowireEntity) Id() int64 {
	return t.id
}
func (t *ExampleAutowireEntity) Name() string {
	return t.name
}
func (t *ExampleAutowireEntity) Info() string {
	return t.info
}
func (t *ExampleAutowireEntity) DailyTime() time.Time {
	return t.dailyTime
}
func (t *ExampleAutowireEntity) Equips() map[int]string {
	return t.equips
}
func (t *ExampleAutowireEntity) Grade() int32 {
	return t.grade
}
func (t *ExampleAutowireEntity) Num() int64 {
	return t.num
}
func (t *ExampleAutowireEntity) Has() bool {
	return t.has
}
func (t *ExampleParent) SomeInfo() string {
	return t.someinfo
}
func (t *ExampleAutowireEntity) SetName(name string) {
	t.name = name
}
func (t *ExampleAutowireEntity) SetNum(num int64) {
	t.num = num
}
func (t *ExampleAutowireEntity) SetHas(has bool) {
	t.has = has
}
func (t *ExampleAutowireEntity) SetSomeInfo(someinfo string) {
	t.someinfo = someinfo
}

func TestExampleAutowireEntity(sqlDB *sql.DB) {
	idao := dao.NewSplitBaseDao[*ExampleAutowireEntity](sqlDB, &ExampleAutowireEntity{})
	fmt.Println(idao.Sql(&ExampleAutowireEntity{}))

	var exampleEntity1 ExampleAutowireEntity
	exampleEntity1.SetName("AAAA")
	exampleEntity1.SetNum(9875451)
	exampleEntity1.SetHas(true)
	exampleEntity1.SetSomeInfo("skdjal4w5e4")
	exampleEntity1.LongTime = time.Now().UnixMilli()
	exampleEntity1.StructInfo = ExampleStruct{Identity: 1, Level: 12, Other: "YY"}
	exampleEntity1.StructSlice = make([]ExampleStruct, 0)
	exampleEntity1.StructSlice = append(exampleEntity1.StructSlice, ExampleStruct{Identity: 1, Level: 21, Other: "YY1"})
	exampleEntity1.StructSlice = append(exampleEntity1.StructSlice, ExampleStruct{Identity: 2, Level: 22, Other: "YY2"})

	exampleEntity1.StructMap = make(map[int32]ExampleStruct)
	exampleEntity1.StructMap[1] = ExampleStruct{Identity: 1, Level: 21, Other: "YY1"}
	exampleEntity1.StructMap[2] = ExampleStruct{Identity: 2, Level: 21, Other: "YY1"}

	exampleEntity1.IntSlice = make([]int32, 0)
	exampleEntity1.IntSlice = append(exampleEntity1.IntSlice, 7)
	exampleEntity1.IntSlice = append(exampleEntity1.IntSlice, 8)
	exampleEntity1.IntSlice = append(exampleEntity1.IntSlice, 9)

	exampleEntity1.StringSlice = make([]string, 0)
	exampleEntity1.StringSlice = append(exampleEntity1.StringSlice, "A1")
	exampleEntity1.StringSlice = append(exampleEntity1.StringSlice, "A2")

	idao.Insert(&exampleEntity1)
	fmt.Println(exampleEntity1.Id())

	exampleEntity1.SetName("PPPPPP")
	idao.Update(&exampleEntity1)
}
